package adv8;

import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLCreate {

	public static void main(String[] args) throws Exception {
		// 从头开始创建一个xml
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File("xueyuan.xml"));
		} catch (Exception e) {
			System.out.println("文件不存在，或者文件不是xml格式");
			// 关键行
			doc = DocumentHelper.createDocument();
			Element root = doc.addElement("javaclass");
			root.addAttribute("className", "java63");
			Element s1 = root.addElement("student");
			s1.addAttribute("kgcno", "1111@qq.com");
			s1.addAttribute("sex", "男");
			s1.addElement("qq").setText("11113");
			s1.addElement("name").setText("尼古拉斯凯奇");
			s1.addElement("telephone").setText("131333333");
			s1.addElement("addr").setText("new york");
			s1.addElement("school").setText("高中毕业");

			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			XMLWriter xm = new XMLWriter(new FileWriter("xueyuan.xml"), format);
			xm.write(doc);
			xm.close();// 关闭
			System.out.println("修改成功");
		}
	}

}
